<script>
	var companyId = ''
	var identity = ''
	var idx = ''
	var pid = ''
	import commonUtil from '@/components/utils/format.js';
	import common from '@/components/utils/common.js';
	import {login,ulogin} from '@/request/api.js'
	export default {
		onLaunch: function() {
			console.log('App Launch')
			// #ifdef H5
				companyId = commonUtil.GetQueryString('companyId')
				identity = commonUtil.GetQueryString('identity')//当前登录身份
				idx = commonUtil.GetQueryString('id')//用户的id
				pid = commonUtil.GetQueryString('pid')//分享用户的id
				if(pid){
					uni.setStorageSync('pid',pid)
				}
				if(identity){
					uni.setStorageSync('identity',identity)
				}
				if(companyId){
					uni.setStorageSync('companyId',companyId)
					this.seWxappid();
				}else{
					this.seWxappid();
					// uni.showModal({
					// 	title: '提示',
					// 	content: '未查询到医馆，请检查链接是否正确！',
					// 	showCancel: false, //没有取消按钮的弹框
					// 	success: function(res) {
					// 		if (res.confirm) {
					// 			console.log('用户点击确定');
					// 		}
					// 	}
					// });
				}
			// #endif
		},
		globalData: {
			baseUrl: 'http://wsfs.jm120.net/api',
			baseUrlImg: 'http://wsfs.jm120.net',
			// baseUrl: 'http://192.168.0.3:11118/api',
			// baseUrlImg: 'http://192.168.0.3:11118'
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			// 公众号授权
			seWxappid() {
				// let appid = 'wxbaa9464486bfb083'; //测试账号
				let appid = 'wxbdba8f76d74f5799';//正式
				let code = common.GetUrlParame('code');
				if (!code) {
					console.log('开始授权');
					const local = location.href;
					if (process.env.NODE_ENV === 'development') {
						console.log('开发环境');
						window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
							'&redirect_uri=' + encodeURIComponent(local) +
							'&response_type=code'+'&scope=snsapi_userinfo&state=STATE#wechat_redirect';
					} else {
						console.log('生产环境');
						window.location.href =
							'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
							appid +
							'&redirect_uri=' +
							encodeURIComponent(local) +
							'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
					}
				} else {
					if(companyId){
						this.getWechatUserInfo(code);
					}else{
						this.getWechatUserInfo2(code);
					}
				}
				try {
					console.log('appid******************未进入', appid);
				} catch (e) {
					console.log(e);
				}
			},
			/* ********************************************************* */
			// 公众号获取用户信息
			getWechatUserInfo(code) {
				var that = this;
				let param = {
					company_id:uni.getStorageSync('companyId'),
					code:code
				}
				login(param).then(res => {
					uni.setStorageSync('gz',res.data.gz)
					uni.setStorageSync('openid',res.data.openid)
					//没有账号，去注册或者登录
					// 账号单身份判断
					if(res.data.reg == 1){
						if(identity == 2){//医生登录
							uni.reLaunch({
								url:'/pages/login/login2'
							})
						}else{
							uni.reLaunch({
								url:'/pages/register/register'
							})
						}
					}else{
						if(identity == 2){
							if(uni.getStorageSync('doctorToken')){
								uni.setStorageSync('token',uni.getStorageSync('doctorToken'))
								uni.setStorageSync('doctorToken',uni.getStorageSync('doctorToken'))
								uni.setStorageSync('identity',2)
								uni.reLaunch({
									url:'/pages/index/index'
								})
							}else{
								uni.reLaunch({
									url:'/pages/login/login2'
								})
							}
						}else{
							if(res.data.member){
								uni.setStorageSync('member',res.data.member)
								uni.setStorageSync('token',res.data.member.login_token)
								uni.setStorageSync('identity',1)
								if(idx){
									uni.reLaunch({
										url:'/pages/myCode/detail?id='+idx
									})
								}else{
									uni.reLaunch({
										url:'/pages/index/index'
									})
								}
							}
						}
					}
				})
			},
			getWechatUserInfo2(code) {
				let param = {
					code:code
				}
				ulogin(param).then(res => {
					if(res.code == 200){
						uni.clearStorage()
						window.location.href = res.data.url;
					}else{
						uni.showModal({
							title: '提示',
							content: res.msg,
							showCancel: false, //没有取消按钮的弹框
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								}
							}
						});
					}
				})
			}
		}
	}
</script>

<style>
	view,
	scroll-view,
	input {
		box-sizing: border-box;
	}

	::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
		color: transparent;
	}
	
	/deep/ .uni-picker-container .uni-picker-item {
		color: red;
		font-weight: bold;
	}

	.content {
		width: 100vw;
		/* height: calc(100vh - 100upx); */
		/* height: calc(100vh); */
		overflow: auto;
	}

	.fullContent {
		width: 100vw;
		height: 100vh;
		overflow: auto;
	}

	/* // 左右30upx padding */
	.pd30 {
		padding: 0 30upx;
	}

	/* // 白色背景 */
	.bgw {
		background: #FFFFFF;
	}

	.bgf8 {
		background: #f8f8f8;
	}

	/* // placeholder样式 */
	.pcs {
		font-size: 28upx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		line-height: 40px;
		color: #CCCCCC;
		opacity: 1;
	}
	
	.pcs2 {
		font-size: 28upx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #CCCCCC;
		opacity: 1;
	}
	
	.red{
		color: #F86C53;
	}

	/* // 按钮样式 */
	.btnStyle {
		width: 690upx;
		height: 88upx;
		background: #F86C53;
		opacity: 1;
		border-radius: 44upx;
		font-size: 36upx;
		font-family: Source Han Sans CN;
		line-height: 88upx;
		color: #FFFFFF;
		text-align: center;
		margin: 0 auto;
	}

	.uniFlex {
		display: flex;
		align-items: center;
	}

	.uniFlex2 {
		display: flex;
	}

	.uniBtn {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.uniBtn2 {
		display: flex;
		justify-content: space-between;
	}

	.uniCenter {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.uniEnd{
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}
	.uniAround{
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
</style>
